package com.smalls.newvideotwo.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by hs on 2018/3/8.
 */

public class Tools {

    public static List<String> removeDuplicateString(List<String> list)
    {
        Set set = new LinkedHashSet<String>();
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    public static List<Integer> removeDuplicateInt(List<Integer> list)
    {
        Set set = new LinkedHashSet<Integer>();
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    public static boolean chmodPath(String path) {
        boolean issuccess = false;
        Process process = null;

        try {
            process = Runtime.getRuntime().exec("chmod -R 777 " + path);

            return process.waitFor() == 0;
        } catch (Exception e) {
            e.printStackTrace();
            issuccess = false;
        }
        return issuccess;
    }

    public static void setRelativeLayoutParam(View v, int w, int h, int l, int top) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
        if(l!=-1) {
            params.leftMargin = (int) MeasureUtil.getWidthSize(l);
        }
        if(top!=-1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }
        if(w!=-1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if(h!=-1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }

    public static void setRelativeLayoutParam2(View v, int w, int h, int r,int top) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
        if(r!=-1) {
            params.rightMargin = (int) MeasureUtil.getWidthSize(r);
        }
        if(top!=-1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }
        if(w!=-1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if(h!=-1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }
	
	    public static void setRelativeLayoutParam3(View v, int w, int h, int r, int bottom) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
        if (r != -1) {
            params.rightMargin = (int) MeasureUtil.getWidthSize(r);
        }
        if (bottom != -1) {
            params.bottomMargin = (int) MeasureUtil.getHeightSize(bottom);
        }
        if (w != -1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if (h != -1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }

    public static void setRelativeLayoutParamAll(View v, int w, int h, int l, int top, int r, int bottom) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
        if (l != -1) {
            params.leftMargin = (int) MeasureUtil.getWidthSize(l);
        }
        if (top != -1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }

        if (r != -1) {
            params.rightMargin = (int) MeasureUtil.getWidthSize(r);
        }
        if (bottom != -1) {
            params.bottomMargin = (int) MeasureUtil.getHeightSize(bottom);
        }

        if (w != -1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if (h != -1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }


    public static void setLinearLayoutParam(View v, int w, int h, int l,int top) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
        if(l!=-1) {
            params.leftMargin = (int) MeasureUtil.getWidthSize(l);
        }
        if(top!=-1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }
        if(w!=-1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if(h!=-1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }

    public static void setLinearLayoutParam2(View v, int w, int h, int r,int top) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
        if(r!=-1) {
            params.rightMargin = (int) MeasureUtil.getWidthSize(r);
        }
        if(top!=-1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }
        if(w!=-1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if(h!=-1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }
	
    public static void setLinearLayoutParam3(View v, int w, int h, int r, int bottom) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
        if (r != -1) {
            params.rightMargin = (int) MeasureUtil.getWidthSize(r);
        }
        if (bottom != -1) {
            params.bottomMargin = (int) MeasureUtil.getHeightSize(bottom);
        }
        if (w != -1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if (h != -1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }
	public static void setLinearLayoutParamAll(View v, int w, int h, int l, int top, int r, int bottom) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
        if (l != -1) {
            params.leftMargin = (int) MeasureUtil.getWidthSize(l);
        }
        if (top != -1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }
        if (r != -1) {
            params.rightMargin = (int) MeasureUtil.getWidthSize(r);
        }
        if (bottom != -1) {
            params.bottomMargin = (int) MeasureUtil.getHeightSize(bottom);
        }
        if (w != -1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if (h != -1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }

    public static void setFrameLayoutParam(View v, int w, int h, int l,int top) {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) v.getLayoutParams();
        if(l!=-1) {
            params.leftMargin = (int) MeasureUtil.getWidthSize(l);
        }
        if(top!=-1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }
        if(w!=-1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if(h!=-1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }

    public static void setFrameLayoutParam2(View v, int w, int h, int r, int b) {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) v.getLayoutParams();
        if (r != -1) {
            params.rightMargin = (int) MeasureUtil.getWidthSize(r);
        }
        if (b != -1) {
            params.bottomMargin = (int) MeasureUtil.getHeightSize(b);
        }
        if (w != -1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if (h != -1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }

    public static void setGridLayoutManagerLayoutParam(View v, int w, int h, int l, int top) {
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) v.getLayoutParams();
        if (l != -1) {
            params.leftMargin = (int) MeasureUtil.getWidthSize(l);
        }
        if (top != -1) {
            params.topMargin = (int) MeasureUtil.getHeightSize(top);
        }
        if (w != -1) {
            params.width = (int) MeasureUtil.getWidthSize(w);
        }
        if (h != -1) {
            params.height = (int) MeasureUtil.getHeightSize(h);
        }
        v.setLayoutParams(params);
    }

    public static void setPadding(View v, int left, int right, int top, int bottom){
        v.setPadding((int) MeasureUtil.getWidthSize(left),(int) MeasureUtil.getWidthSize(right),(int) MeasureUtil.getHeightSize(top),(int) MeasureUtil.getHeightSize(bottom));
    }

    /**
     * 判断服务是否开启
     *
     * @return
     */
    public static boolean isServiceRunning2(Context context, String ServiceName) {
        if (("").equals(ServiceName) || ServiceName == null)
            return false;
        ActivityManager myManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
                .getRunningServices(30);
        for (int i = 0; i < runningService.size(); i++) {
            if (runningService.get(i).service.getClassName().toString()
                    .equals(ServiceName)) {
                return true;
            }
        }
        return false;
    }
}
